package com.duoduo.剑指Offer;

/**
 * @program: algorithm
 * @description: 替换空格06
 * 请实现一个函数，把字符串 s 中的每个空格替换成"%20"。
 *
 * 示例 1：
 *
 * 输入：s = "We are happy."
 * 输出："We%20are%20happy."
 *
 * @author: chaidl  ！
 * @create: 2022-05-22 10:07
 */
public class 替换空格06 {
	public String replaceSpace(String s) {
		s=s.replace(" ","%20");
		return s;
	}
	public String replaceSpace2(String s) {
		StringBuilder stringBuilder=new StringBuilder();
		for (char c : s.toCharArray()) {
			if (c==' '){
				stringBuilder.append("%20");
			}else {
				stringBuilder.append(c);
			}
		}
		return stringBuilder.toString();
	}
	public String replaceSpace3(String s) {
		int length = s.length();
		int j=0;
		char[] arr=new char[length*3];
		for (int i = 0; i < length; i++) {
			if (s.charAt(i) == ' '){
				arr[j++]='%';
				arr[j++]='2';
				arr[j++]='0';
			}else {
				arr[j++]=s.charAt(i);
			}
		}
		return new String(arr,0,j);
	}
}
